package com.github.tifezh.kchartlib.chart;

import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.support.annotation.NonNull;

import com.github.tifezh.kchartlib.chart.base.IMinuteAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class BaseMinuteAdapter<T> implements IMinuteAdapter<T> {

    class MinuteData {
        long startTime;
        long endTime;
        long breakStartTime;
        long breakEndTime;

        public void setStartTime(long startTime) {
            this.startTime = startTime;
        }

        public void setEndTime(long endTime) {
            this.endTime = endTime;
        }

        public void setBreakStartTime(long breakStartTime) {
            this.breakStartTime = breakStartTime;
        }

        public void setBreakEndTime(long breakEndTime) {
            this.breakEndTime = breakEndTime;
        }

        int getMaxCount() {
            return (int) (endTime - startTime - (breakEndTime - breakStartTime));
        }
    }

    private MinuteData minuteData = null;

    private final List<T> data = new ArrayList<>();

    private final DataSetObservable mDataSetObservable = new DataSetObservable();

    public BaseMinuteAdapter(String startTime, String endTime, String breakStartTime, String breakEndTime) {
        minuteData = new MinuteData();
    }

    @Override
    public int getMaxCount() {
        return minuteData.getMaxCount();
    }

    @Override
    public List<T> getData() {
        return data;
    }

    @Override
    public T getItem(int position) {
        return data.get(position);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public void changeItem(int index, @NonNull T item) {
        Utils.checkNull(item, "item == null");
        data.set(index, item);
        notifyDataSetChanged();
    }

    @Override
    public void setNewData(Collection<? extends T> newData) {
        data.clear();
        if (newData != null) {
            data.addAll(newData);
        }
        notifyDataSetChanged();
    }

    @Override
    public void addData(@NonNull T item) {
        Utils.checkNull(item, "item == null");
        data.add(0, item);
        notifyDataSetChanged();
    }

    @Override
    public void addData(Collection<? extends T> newData) {
        if (newData != null) {
            data.addAll(0, newData);
            notifyDataSetChanged();
        }
    }

    public void notifyDataSetChanged() {
        if (getCount() > 0) {
            mDataSetObservable.notifyChanged();
        } else {
            mDataSetObservable.notifyInvalidated();
        }
    }


    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.registerObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.unregisterObserver(observer);
    }
}
